package com.manage.servlet;

import com.manage.pojo.ResultData;
import com.manage.service.UsersService;
import com.manage.service.impl.UsersServiceImpl;
import com.manage.utils.CookieUtils;
import com.manage.utils.VerifyCode;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@WebServlet("/users")
public class UsersServlet extends BaseServlet{
    public ResultData login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String username = request.getParameter("username");
        String password=request.getParameter("password");
        String verifyCode = request.getParameter("captcha");
        String rememberMe = request.getParameter("rememberMe");

        String text = (String) request.getSession().getAttribute("text");
        //2.处理请求
        UsersService userService=new UsersServiceImpl();
        ResultData resultData = userService.loginCheck(username, password, verifyCode, text,rememberMe);
        if (resultData.getCode()==0) {
            if (rememberMe != null) {

                request.getSession().setAttribute("username",username);
                request.getSession().setAttribute("password",password);
                CookieUtils.setCookie("username",username,response,request);
                CookieUtils.setCookie("password",password,response,request);
            }else {
                CookieUtils.removeCookie("username",request,response);
                CookieUtils.removeCookie("password",request,response);
            }
        }
        //3.响应请求
        return  resultData;
    }

    public void useVerifyCode(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //2.处理请求
        VerifyCode verifyCode=new VerifyCode();
        BufferedImage image = verifyCode.getImage();
        String text = verifyCode.getText();
        request.getSession().setAttribute("text", text);

        //3.响应请求
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    public ResultData register(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //1.获取数据
        String username = request.getParameter("username");
        String password=request.getParameter("password");
        String verifyCode = request.getParameter("captcha");

        String text = (String) request.getSession().getAttribute("text");
        //2.处理请求
        UsersService userService=new UsersServiceImpl();
        ResultData resultData = userService.registerCheck(username, password, verifyCode, text);

        //3.响应请求
        return  resultData;
    }

    public void loginOut(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        //2.处理请求
        request.getSession().removeAttribute("username");
        request.getSession().removeAttribute("password");
        //3.响应请求
        return  ;
    }
}
